/**
 * Created by Administrator on 2017/9/1.
 */
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

// root state object.
// each Vuex instance is just a single state tree.
const state = {
  nickName: '',
  cartCount: 0
}

// mutations are operations that actually mutates the state.
// each mutation handler gets the entire state tree as the
// first argument, followed by additional payload arguments.
// mutations must be synchronous and can be recorded by plugins
// for debugging purposes.
const mutations = {
  setNickName(state, nickName) {
    state.nickName = nickName
  },
  setCartCount(state, cartCount) {
    state.cartCount = cartCount
  },
  addCartCount(state) {
    state.cartCount++
  },
  minusCartCount(state) {
    state.cartCount--
  }
}

// actions are functions that cause side effects and can involve
// asynchronous operations.
const actions = {
  getCartCount({commit}) {
    console.log(123)
    axios.get('/user/getCartCount').then((res) => {
      if (res.data.status == 0) {
        commit('setCartCount', res.data.result)
      }
    })
  }
}

// A Vuex instance is created by combining the state, mutations, actions,
// and getters.
export default new Vuex.Store({
  state,
  mutations,
  actions
})
